package com.ruoyi.web.controller.tableSettings;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.util.StoreIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TableInfo;
import com.ruoyi.system.service.ITableInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 桌台信息 Controller
 *
 * @author ruoyi
 * @date 2025-05-07
 */
@Api(tags = "桌台信息")
@RestController
@RequestMapping("/system/tableInfo")
public class TableInfoController extends BaseController
{
    @Autowired
    private ITableInfoService tableInfoService;

    @Autowired
    private StoreIdUtils storeIdUtils;

    /**
     * 查询桌台信息列表
     */
    @ApiOperation("查询桌台信息列表")
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TableInfo tableInfo)
    {
        startPage();
        List<TableInfo> list = tableInfoService.selectTableInfoList(tableInfo);
        return getDataTable(list);
    }

    /**
     * 导出桌台信息列表
     */
    @ApiOperation("导出桌台信息列表")
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "桌台信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TableInfo tableInfo)
    {
        List<TableInfo> list = tableInfoService.selectTableInfoList(tableInfo);
        ExcelUtil<TableInfo> util = new ExcelUtil<TableInfo>(TableInfo.class);
        util.exportExcel(response, list, "桌台信息数据");
    }

    /**
     * 通过id获取桌台信息详细信息
     */
    @ApiOperation("通过id获取桌台信息详细信息")
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tableInfoService.selectTableInfoById(id));
    }

    /**
     * 新增桌台信息
     */
    @ApiOperation("新增桌台信息")
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "桌台信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TableInfo tableInfo)
    {
        return toAjax(tableInfoService.insertTableInfo(tableInfo));
    }

    /**
     * 修改桌台信息
     */
    @ApiOperation("修改桌台信息")
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "桌台信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TableInfo tableInfo)
    {
        return toAjax(tableInfoService.updateTableInfo(tableInfo));
    }

    /**
     * 通过id批量删除桌台信息
     */
    @ApiOperation("通过id批量删除桌台信息")
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "桌台信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public AjaxResult remove(@RequestBody List<Long> ids)
    {
        if(CollectionUtils.isEmpty(ids)) {
            return AjaxResult.error("请提供要删除的ID列表");
        }
        return toAjax(tableInfoService.deleteTableInfoByIds(ids));
    }


    /**
     * 根据桌台分类Id 获取桌台信息
     */
    @ApiOperation("根据桌台分类Id 获取桌台信息")
    @GetMapping("/getTableInfoByTableCategoryId")
    public AjaxResult getTableInfoByTableCategoryId(Long tableCategoryId)
    {
        LambdaQueryWrapper<TableInfo> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TableInfo::getTableCategoryId, tableCategoryId);
        wrapper.eq(TableInfo::getStoreId,storeIdUtils.getStoreId());
        List<TableInfo> tableInfoList = tableInfoService.list(wrapper);
        return AjaxResult.success(tableInfoList);
    }

}

